//
//  TYKeyboardManager.m
//  Somago
//
//  Created by 谢航宇_WORK on 2021/10/22.
//  Copyright © 2021 juniu. All rights reserved.
//

#import "TYKeyboardManager.h"
#import <objc/runtime.h>
#import "Masonry.h"
@implementation TYKeyboardManager

static TYKeyboardManager * _TYKeyboardManager = nil;
+ (instancetype)instance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _TYKeyboardManager = [[TYKeyboardManager alloc]init];
        [[NSNotificationCenter defaultCenter]addObserver:_TYKeyboardManager selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:_TYKeyboardManager selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:_TYKeyboardManager selector:@selector(textFieldDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:_TYKeyboardManager selector:@selector(textFieldDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
    });
    return _TYKeyboardManager;
}

///键盘打开
- (void)keyboardWillShow:(NSNotification *)noty {
    self.keyboardRect = [[[noty userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
//    for (UIWindow * window in [UIApplication sharedApplication].windows) {
//        if ([self isKeyboardWindow:window]) {
//
//        }
//    }
    [[[UIApplication sharedApplication]delegate].window addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo([[UIApplication sharedApplication]delegate].window);
    }];
}
///键盘关闭
- (void)keyboardWillHide:(NSNotification *)noty {
    [self removeFromSuperview];
}

- (BOOL)isKeyboardWindow:(UIWindow *)window {
    /*
     iOS 6/7:
     UITextEffectsWindow
        UIPeripheralHostView << keyboard
     
     iOS 8:
     UITextEffectsWindow
        UIInputSetContainerView
            UIInputSetHostView << keyboard
     
     iOS 9:
     UIRemoteKeyboardWindow
        UIInputSetContainerView
            UIInputSetHostView << keyboard
     */
    if (!window) return NO;
    
    // Get the window
    NSString *windowName = NSStringFromClass(window.class);
    if ([windowName hasSuffix:@"TextEffectsWindow"]) return YES;
    if ([windowName hasSuffix:@"RemoteKeyboardWindow"]) return YES;
    return NO;
}

- (void)textFieldDidBeginEditing:(NSNotification *)noty {
    self.responseTextField = noty.object;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    static BOOL isCalculate = NO;
    if (isCalculate) {
        return nil;
    }
    isCalculate = YES;
    UIView * view = [[[UIApplication sharedApplication]delegate].window hitTest:point withEvent:event];
    UIView * currenteView = self.responseTextField;
    isCalculate = NO;
    CGRect rect = [self.responseTextField convertRect:self.responseTextField.bounds toView:(UIView *)([[UIApplication sharedApplication]delegate].window)];//view相对于window位置
    if (point.y < self.keyboardRect.origin.y && !CGRectContainsPoint(rect, point) && ![view isKindOfClass:[UITextField class]] && ![view isKindOfClass:[UITextView class]] && currenteView == self.responseTextField && view.hiddenEndEditing == NO) {
        [self.responseTextField endEditing:YES];
    }
    return nil;
}

#pragma mark ----初始化
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _TYKeyboardManager = [super allocWithZone:zone];
    });
    return _TYKeyboardManager;
}
- (instancetype)init{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _TYKeyboardManager = [super init];
    });
    return _TYKeyboardManager;
}
-(id)copy{
    return self;
}

-(id)mutableCopy{
    return self;
}

-(id)copyWithZone:(NSZone *)zone{
    return self;
}

-(id)mutableCopyWithZone:(NSZone *)zone{
    return self;
}

@end


@implementation UIView (Extension)
- (BOOL)hiddenEndEditing {
    return [objc_getAssociatedObject(self, @"hiddenEndEditing") integerValue];
}

- (void)setHiddenEndEditing:(BOOL)hiddenEndEditing {
    objc_setAssociatedObject(self, @"hiddenEndEditing", @(hiddenEndEditing), OBJC_ASSOCIATION_ASSIGN);
}
 
@end
